//
//  UIImage(KsiFunction).m
//  WeddingPhoto
//
//  Created by cnksi on 11-8-10.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "KsiUIImage.h"

@implementation UIImage (KsiFunction)

- (id)initWithContentsOfResolutionIndependentFile:(NSString *)path {
    if ( [UIScreen instancesRespondToSelector:@selector(scale)] && (int)[[UIScreen mainScreen] scale] == 2.0 ) {
        NSString *path2x = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent: [NSString stringWithFormat:@"%@@2x.%@", 
                                                                                                      [[path lastPathComponent] stringByDeletingPathExtension], 
                                                                                                      [path pathExtension]]];
        
        if ( [[NSFileManager defaultManager] fileExistsAtPath:path2x] ) {
            return [self initWithContentsOfFile:path2x];
        }
    }
    
    return [self initWithContentsOfFile:path];
}

+ (UIImage*)imageWithContentsOfResolutionIndependentFile:(NSString *)path {
    return [[[UIImage alloc] initWithContentsOfResolutionIndependentFile:path] autorelease];
}

+ (UIImage *)KsiThumbImageWithFullImageName:(NSString *)imageName withSize:(CGSize)size withMode:(KsiThumbImageMode)mode{
    NSString* __newImageName = nil;
    if ( [UIScreen instancesRespondToSelector:@selector(scale)] && (int)[[UIScreen mainScreen] scale] == 2.0 ){
        __newImageName = [NSString stringWithFormat:@"%.2fx%.2f_%d_%@@2x.%@", size.width, size.height, mode, [[imageName lastPathComponent] stringByDeletingPathExtension], [imageName pathExtension]];
    }else{
        __newImageName = [NSString stringWithFormat:@"%.2fx%.2f_%d_%@", size.width, size.height, mode, [imageName lastPathComponent]];
    }
    UIImage *__image = nil;
    //直接返回缓存图片
    __image = [UIImage cachedImageWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingPathComponent:__newImageName]];
    if (__image != nil) {
        return __image;
    }    

    
    //生成缓存图片
    __image = [UIImage imageWithContentsOfFile:imageName];
    if (!__image) {
        return nil;
    }
    
    UIImage *__newImage = nil;
    switch (mode) {         
        case KsiThumbImageModePortion:{
            CGRect rect = CGRectMake((__image.size.width - size.width) / 2, (__image.size.height - size.height) / 2, size.width, size.height);
            CGImageRef __imageRef = CGImageCreateWithImageInRect([__image CGImage], rect);
            __newImage = [UIImage imageWithCGImage:__imageRef]; 
            CGImageRelease(__imageRef);
            break;
        }
        case KsiThumbImageModeResize:{
            CGSize __oldSize = __image.size;
            CGRect __rect = CGRectZero;
            if (size.width / size.height > __oldSize.width / __oldSize.height) {
                __rect.size.width = size.height * __oldSize.width / __oldSize.height;
                __rect.size.height = size.height;
                __rect.origin.x = (size.width - __rect.size.width) / 2;
                __rect.origin.y = 0;
            }else{
                __rect.size.width = size.width;
                __rect.size.height = size.width * __oldSize.height / __oldSize.width;
                __rect.origin.x = 0;
                __rect.origin.y = (size.height - __rect.size.height) / 2;
            }
            
            UIGraphicsBeginImageContext(size);
            CGContextRef __context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(__context, [UIColor clearColor].CGColor);
            UIRectFill(CGRectMake(0, 0, size.width, size.height));
            [__image drawInRect:__rect];
            __newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            break;
        }
        case KsiThumbImageModeWidthFirst:{
            CGSize __oldSize = __image.size;
            CGRect __rect = CGRectZero;
            __rect.size.width = size.width;
            __rect.size.height = size.width * __oldSize.height / __oldSize.width;
            __rect.origin.x = 0;
            __rect.origin.y = (size.height - __rect.size.height) / 2;
            
            UIGraphicsBeginImageContext(size);
            CGContextRef __context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(__context, [UIColor clearColor].CGColor);
            UIRectFill(CGRectMake(0, 0, size.width, size.height));
            [__image drawInRect:__rect];
            __newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            break;
        }
        case KsiThumbImageModeHeightFirst:{
            CGSize __oldSize = __image.size;
            CGRect __rect = CGRectZero;
            __rect.size.width = size.height * __oldSize.width / __oldSize.height;
            __rect.size.height = size.height;
            __rect.origin.x = (size.width - __rect.size.width) / 2;
            __rect.origin.y = 0;
            
            UIGraphicsBeginImageContext(size);
            CGContextRef __context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(__context, [UIColor clearColor].CGColor);
            UIRectFill(CGRectMake(0, 0, size.width, size.height));
            [__image drawInRect:__rect];
            __newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            break;
        }
        case KsiThumbImageModeFull:{
            UIGraphicsBeginImageContext(size);
            [__image drawInRect:CGRectMake(0, 0, size.width, size.height)];            
            __newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            break;
        }
        case KsiThumbImageModeAuto:{
            CGSize __oldSize = __image.size;
            CGRect __rect = CGRectZero;
            if (size.width / size.height > __oldSize.width / __oldSize.height) {
                __rect.size.width = size.width;
                __rect.size.height = size.width * __oldSize.height / __oldSize.width;
                __rect.origin.x = 0;
                __rect.origin.y = (size.height - __rect.size.height) / 2;
            }else{
                __rect.size.width = size.height * __oldSize.width / __oldSize.height;
                __rect.size.height = size.height;
                __rect.origin.x = (size.width - __rect.size.width) / 2;
                __rect.origin.y = 0;
            }
            
            UIGraphicsBeginImageContext(size);
            CGContextRef __context = UIGraphicsGetCurrentContext();
            CGContextSetFillColorWithColor(__context, [UIColor clearColor].CGColor);
            UIRectFill(CGRectMake(0, 0, size.width, size.height));
            [__image drawInRect:__rect];
            __newImage = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            break;
        }
        default:
            break;
    }
    
    if (__newImage) {
        NSData *__imageData = [NSData dataWithData:UIImagePNGRepresentation(__newImage)];
        [__imageData writeToFile:[NSTemporaryDirectory() stringByAppendingPathComponent:__newImageName] atomically:YES];
    }
    
    return __newImage;
}
+(UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size {
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
@end
